package com.hb.web;

import com.hb.domain.Spitter;
import com.hb.domain.Spittle;
import com.hb.repository.SpitterRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 如果在控制器类上使用 @RestController 来代替 @Controller 的话，Spring 将会为该控制器的所有处理方法应用消息转换功能。我们不必为每个方法都添加 @ResponseBody 了。
 * 但是 @RequestBody 还需要
 */
@RestController
@RequestMapping("/spitter")
public class SpitterController {
    private SpitterRepository spitterRepository;

    @Autowired
    public SpitterController(SpitterRepository spitterRepository) {
        this.spitterRepository = spitterRepository;
    }

    /**
     * 由于使用了 @RestController，所以这个处理器方法不需要 @ResponseBody 了。
     * @param spitter
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = "application/json")
    public Spitter saveSpitter(@RequestBody Spitter spitter) {
        return spitterRepository.save(spitter);
    }
}
